package com.wuye.web.system.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.wuye.utils.Result;
import com.wuye.web.system.entity.*;
import com.wuye.web.system.service.ISysMenuService;
import com.wuye.web.system.service.ISysRoleService;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 角色表 前端控制器
 * </p>
 *
 * @author 孤筏
 * @since 2023-07-26
 */
@RestController
@RequestMapping("/api/role")
public class SysRoleController {
    @Resource
    private ISysRoleService roleService;
    @Resource
    private ISysMenuService menuService;

    @ApiOperation("角色列表")
    @GetMapping("/list")
    public Result listRoles(SysRoleParam sysRoleParam){
        IPage<SysRole> page = roleService.listRoles(sysRoleParam);
        return Result.success(page);
    }

    @ApiOperation("添加角色")
    @PostMapping
    public Result addRole(@RequestBody SysRole sysRole){
        boolean save = roleService.save(sysRole);
        if (save){
            return Result.success();
        }
        return Result.error(500,"添加失败");
    }

    @ApiOperation("修改角色")
    @PutMapping
    public Result updateRole(@RequestBody SysRole sysRole){
        boolean update = roleService.updateById(sysRole);
        if (update){
            return Result.success();
        }
        return Result.error(500,"修改失败");
    }

    @ApiOperation("删除角色")
    @DeleteMapping("/{roleId}")
    public Result deleteRole(@PathVariable Integer roleId){
        boolean remove = roleService.removeById(roleId);
        if (remove){
            return Result.success();
        }
        return Result.error(500,"删除失败");
    }

    @ApiOperation("获得分配权限菜单")
    @GetMapping("/getAssignTree/{roleId}")
    public Result getAssignTree(@PathVariable("roleId") Integer roleId){
        CheckMenuTreeVo checkMenuTreeVo = menuService.getAssignTree(roleId);
        return Result.success(checkMenuTreeVo);
    }
    @ApiOperation("保存角色拥有的权限信息")
    @PutMapping("/saveMenuIdsByRoleId")
    public Result saveMenuIdsByRoleId(@RequestBody SaveMenuIdsByRoleIdParam param){
        menuService.saveMenuIdsByRoleId(param);
        return Result.success();
    }

    @ApiOperation("获取所有角色列表")
    @GetMapping("/getList")
    public Result getList(){
        List<SysRole> list = roleService.list();
        return Result.success(list);
    }
}
